<template>
	<view class="add-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="发布话题"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" label="话题" required prop="content">
					<u-input v-model="form.content" type="textarea" placeholder="请输入话题" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>

				<u-form-item class="form-item" label="图片">
					<uni-file-upload style="width: 500rpx" type="image" :limit="9" :files="fileList"></uni-file-upload>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button type="error" @click="confirm">提交</u-button></view>
	</view>
</template>

<script>
import uniFileUpload from '@/components/uni-file-uploader.vue';
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		uniFileUpload
	},
	data() {
		return {
			id: '',
			clientCode: '',
			clientName: '',
			navItem: 1,
			title: '',
			type: '',
			fileList: [],

			type: '',
			typeShow: false,
			typeList: [],

			form: {
				content: ''
				// type: 3,
				// articleType: '',
				// title: ''
			},
			rules: {
				content: [
					{
						required: true,
						message: '请输入话题',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		//this.loadDict('dsxx_3_all', 'typeList');

		if (option && option.id) {
			this.id = option.id;
			//this.loadData();
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		typeConfirm(e) {
			this.type = e[0].label;
			this.form.articleType = e[0].value;
		},
		getFileList(e) {
			this.fileList = e;
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.form.createBy = this.userInfo.sysUser.id;
			} else {
				this.$store.commit('logout');
				this.$msg('请先登录');
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 500);
			}
		},
		loadData() {},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				this[list] = [];
				res.data &&
					res.data.forEach((item) => {
						this[list].push({
							value: item.dictValue,
							label: item.dictName
						});
					});
			});
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				} else {
					let data = {};
					if (this.id) {
						this.form.id = this.id;
					}

					this.form.clientCode = this.clientCode;

					this.form.clientName = this.userInfo.sysUser.clientName;
					this.form.createBy = this.userInfo.sysUser.id;

					data = {
						sqmyBbs: this.form,
						fileList: this.fileList
					};
					this.$request('/sqmybbs/add', data, {
						load: true
					}).then((res) => {
						this.$msg(res.msg);
						setTimeout(() => {
							let pre = this.$prePage();
							if (pre && pre.loadData) {
								pre.loadData('refresh');
							}
							uni.navigateBack();
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.add-view {
	background-color: #fff;
}

.form-box {
	border-top: 20rpx solid $u-border-color;

	.form-item {
		padding: 20rpx 30rpx;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
